import type { CodeType } from '@/types/user.d'
import { OrderType } from '@/enum'
import {
  cancelOrder,
  deleteOrder,
  followOrUnfollow,
  getPrescriptionPic
} from '@/services/consult'
import { getMedicalOrderDetail } from '@/services/order'
import { sendMobileCode } from '@/services/user'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import type { OrderDetail } from '@/types/order'
import {
  showFailToast,
  showImagePreview,
  showSuccessToast,
  showToast,
  type FormInstance
} from 'vant'
import { onMounted, onUnmounted, ref, type Ref } from 'vue'

// 1. 关注和取消关注
export const useFollow = (type: FollowType) => {
  const loading = ref(false)
  // 类型兼容: 一个对象中属性多的可以传递给少的 {a, b, c} => {a, b}
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    try {
      loading.value = true
      await followOrUnfollow(type, item.id)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return { loading, follow }
}

// 2. 展示处方单
export const useShowPrescription = () => {
  const showPrePic = async (id?: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return { showPrePic }
}

// 3. 取消问诊订单
export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      // 改变订单的状态和状态提示文字, 实现无刷新更新
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消问诊订单失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, cancelConsultOrder }
}

// 4. 删除问诊订单
export const useDeleteOrder = (cb: () => void) => {
  const loading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除问诊订单成功')
      // 处理不同的业务
      cb && cb()
    } catch (error) {
      showFailToast('删除问诊订单失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, deleteConsultOrder }
}

// 5. 根基订单id获取药品订单
export const useOrderDetail = (orderId: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    try {
      loading.value = true
      const res = await getMedicalOrderDetail(orderId)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { loading, order }
}

// 6. 获取短信验证码
export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  const time = ref(0) // 记录倒计时
  const formRef = ref<FormInstance>()
  let timer: number
  const send = async () => {
    // ----------发送短信验证码逻辑
    // 4.1 判断是否在倒计时
    if (time.value > 0) return
    // 4.2 判断手机号码是否正确
    await formRef.value?.validate('mobile')
    // 4.3 调用获取短信验证码接口
    const res = await sendMobileCode(mobile.value, type)
    showToast(res.data.code)

    // ------------倒计时逻辑
    time.value = 60
    window.clearInterval(timer)
    timer = window.setInterval(() => {
      time.value--
      if (time.value <= 0) {
        window.clearInterval(timer)
      }
    }, 1000)
  }

  onUnmounted(() => {
    window.clearInterval(timer)
  })

  return { formRef, time, send }
}
